/**
 * Copyleft (C) 2023-2024 maminjie <canpool@163.com>
 * SPDX-License-Identifier: BSD-3-Clause
 **/
#include "mattheme.h"
#include "mattheme_p.h"
#include <QDebug>
#include <QString>
#include <QStringBuilder>

QX_MATERIAL_BEGIN_NAMESPACE

/*!material
 *  \class MatThemePrivate
 *  \internal
 */

MatThemePrivate::MatThemePrivate(MatTheme *q)
    : q_ptr(q)
{
}

MatThemePrivate::~MatThemePrivate()
{
}

/*!
 *  \class MatTheme
 */

MatTheme::MatTheme(QObject *parent) : QObject(parent), d_ptr(new MatThemePrivate(this))
{
    setColor(MK_primary1,        Material::cyan500);
    setColor(MK_primary2,        Material::cyan700);
    setColor(MK_primary3,        Material::lightBlack);
    setColor(MK_accent1,         Material::pinkA200);
    setColor(MK_accent2,         Material::grey100);
    setColor(MK_accent3,         Material::grey500);
    setColor(MK_text,            Material::darkBlack);
    setColor(MK_textAlt,         Material::white);
    setColor(MK_canvas,          Material::white);
    setColor(MK_border,          Material::grey300);
    setColor(MK_disabled1,       Material::minBlack);
    setColor(MK_disabled2,       Material::faintBlack);
    setColor(MK_disabled3,       Material::grey300);
}

MatTheme::~MatTheme()
{
}

QList<QString> MatTheme::colorKeys() const
{
    Q_D(const MatTheme);
    return d->colors.keys();
}

QColor MatTheme::getColor(const QString &key) const
{
    Q_D(const MatTheme);

    if (!d->colors.contains(key)) {
        qWarning() << "A theme color matching the key '" << key << "' could not be found.";
        return QColor();
    }
    return d->colors.value(key);
}

void MatTheme::setColor(const QString &key, const QColor &color)
{
    Q_D(MatTheme);
    d->colors.insert(key, color);
}

void MatTheme::setColor(const QString &key, Material::Color color)
{
    Q_D(MatTheme);
    d->colors.insert(key, this->color(color));
}

QIcon MatTheme::icon(QString category, QString icon)
{
    return QIcon(":/res/icons/" % category % "/svg/production/ic_" % icon % "_24px.svg");
}

const QColor &MatTheme::color(Material::Color color)
{
    static const QColor palette[] = {
        QColor(0xffebee),  //  #ffebee // red
        QColor(0xffcdd2),  //  #ffcdd2
        QColor(0xef9a9a),  //  #ef9a9a
        QColor(0xe57373),  //  #e57373
        QColor(0xef5350),  //  #ef5350
        QColor(0xf44336),  //  #f44336
        QColor(0xe53935),  //  #e53935
        QColor(0xd32f2f),  //  #d32f2f
        QColor(0xc62828),  //  #c62828
        QColor(0xb71c1c),  //  #b71c1c
        QColor(0xff8a80),  //  #ff8a80
        QColor(0xff5252),  //  #ff5252
        QColor(0xff1744),  //  #ff1744
        QColor(0xd50000),  //  #d50000
        QColor(0xfce4ec),  //  #fce4ec // pink
        QColor(0xf8bbd0),  //  #f8bbd0
        QColor(0xf48fb1),  //  #f48fb1
        QColor(0xf06292),  //  #f06292
        QColor(0xec407a),  //  #ec407a
        QColor(0xe91e63),  //  #e91e63
        QColor(0xd81b60),  //  #d81b60
        QColor(0xc2185b),  //  #c2185b
        QColor(0xad1457),  //  #ad1457
        QColor(0x880e4f),  //  #880e4f
        QColor(0xff80ab),  //  #ff80ab
        QColor(0xff4081),  //  #ff4081
        QColor(0xf50057),  //  #f50057
        QColor(0xc51162),  //  #c51162
        QColor(0xf3e5f5),  //  #f3e5f5 // purple
        QColor(0xe1bee7),  //  #e1bee7
        QColor(0xce93d8),  //  #ce93d8
        QColor(0xba68c8),  //  #ba68c8
        QColor(0xab47bc),  //  #ab47bc
        QColor(0x9c27b0),  //  #9c27b0
        QColor(0x8e24aa),  //  #8e24aa
        QColor(0x7b1fa2),  //  #7b1fa2
        QColor(0x6a1b9a),  //  #6a1b9a
        QColor(0x4a148c),  //  #4a148c
        QColor(0xea80fc),  //  #ea80fc
        QColor(0xe040fb),  //  #e040fb
        QColor(0xd500f9),  //  #d500f9
        QColor(0xaa00ff),  //  #aa00ff
        QColor(0xede7f6),  //  #ede7f6 // deepPurple
        QColor(0xd1c4e9),  //  #d1c4e9
        QColor(0xb39ddb),  //  #b39ddb
        QColor(0x9575cd),  //  #9575cd
        QColor(0x7e57c2),  //  #7e57c2
        QColor(0x673ab7),  //  #673ab7
        QColor(0x5e35b1),  //  #5e35b1
        QColor(0x512da8),  //  #512da8
        QColor(0x4527a0),  //  #4527a0
        QColor(0x311b92),  //  #311b92
        QColor(0xb388ff),  //  #b388ff
        QColor(0x7c4dff),  //  #7c4dff
        QColor(0x651fff),  //  #651fff
        QColor(0x6200ea),  //  #6200ea
        QColor(0xe8eaf6),  //  #e8eaf6 // indigo
        QColor(0xc5cae9),  //  #c5cae9
        QColor(0x9fa8da),  //  #9fa8da
        QColor(0x7986cb),  //  #7986cb
        QColor(0x5c6bc0),  //  #5c6bc0
        QColor(0x3f51b5),  //  #3f51b5
        QColor(0x3949ab),  //  #3949ab
        QColor(0x303f9f),  //  #303f9f
        QColor(0x283593),  //  #283593
        QColor(0x1a237e),  //  #1a237e
        QColor(0x8c9eff),  //  #8c9eff
        QColor(0x536dfe),  //  #536dfe
        QColor(0x3d5afe),  //  #3d5afe
        QColor(0x304ffe),  //  #304ffe
        QColor(0xe3f2fd),  //  #e3f2fd // blue
        QColor(0xbbdefb),  //  #bbdefb
        QColor(0x90caf9),  //  #90caf9
        QColor(0x64b5f6),  //  #64b5f6
        QColor(0x42a5f5),  //  #42a5f5
        QColor(0x2196f3),  //  #2196f3
        QColor(0x1e88e5),  //  #1e88e5
        QColor(0x1976d2),  //  #1976d2
        QColor(0x1565c0),  //  #1565c0
        QColor(0x0d47a1),  //  #0d47a1
        QColor(0x82b1ff),  //  #82b1ff
        QColor(0x448aff),  //  #448aff
        QColor(0x2979ff),  //  #2979ff
        QColor(0x2962ff),  //  #2962ff
        QColor(0xe1f5fe),  //  #e1f5fe // lightBlue
        QColor(0xb3e5fc),  //  #b3e5fc
        QColor(0x81d4fa),  //  #81d4fa
        QColor(0x4fc3f7),  //  #4fc3f7
        QColor(0x29b6f6),  //  #29b6f6
        QColor(0x03a9f4),  //  #03a9f4
        QColor(0x039be5),  //  #039be5
        QColor(0x0288d1),  //  #0288d1
        QColor(0x0277bd),  //  #0277bd
        QColor(0x01579b),  //  #01579b
        QColor(0x80d8ff),  //  #80d8ff
        QColor(0x40c4ff),  //  #40c4ff
        QColor(0x00b0ff),  //  #00b0ff
        QColor(0x0091ea),  //  #0091ea
        QColor(0xe0f7fa),  //  #e0f7fa // cyan
        QColor(0xb2ebf2),  //  #b2ebf2
        QColor(0x80deea),  //  #80deea
        QColor(0x4dd0e1),  //  #4dd0e1
        QColor(0x26c6da),  //  #26c6da
        QColor(0x00bcd4),  //  #00bcd4
        QColor(0x00acc1),  //  #00acc1
        QColor(0x0097a7),  //  #0097a7
        QColor(0x00838f),  //  #00838f
        QColor(0x006064),  //  #006064
        QColor(0x84ffff),  //  #84ffff
        QColor(0x18ffff),  //  #18ffff
        QColor(0x00e5ff),  //  #00e5ff
        QColor(0x00b8d4),  //  #00b8d4
        QColor(0xe0f2f1),  //  #e0f2f1 // teal
        QColor(0xb2dfdb),  //  #b2dfdb
        QColor(0x80cbc4),  //  #80cbc4
        QColor(0x4db6ac),  //  #4db6ac
        QColor(0x26a69a),  //  #26a69a
        QColor(0x009688),  //  #009688
        QColor(0x00897b),  //  #00897b
        QColor(0x00796b),  //  #00796b
        QColor(0x00695c),  //  #00695c
        QColor(0x004d40),  //  #004d40
        QColor(0xa7ffeb),  //  #a7ffeb
        QColor(0x64ffda),  //  #64ffda
        QColor(0x1de9b6),  //  #1de9b6
        QColor(0x00bfa5),  //  #00bfa5
        QColor(0xe8f5e9),  //  #e8f5e9 // green
        QColor(0xc8e6c9),  //  #c8e6c9
        QColor(0xa5d6a7),  //  #a5d6a7
        QColor(0x81c784),  //  #81c784
        QColor(0x66bb6a),  //  #66bb6a
        QColor(0x4caf50),  //  #4caf50
        QColor(0x43a047),  //  #43a047
        QColor(0x388e3c),  //  #388e3c
        QColor(0x2e7d32),  //  #2e7d32
        QColor(0x1b5e20),  //  #1b5e20
        QColor(0xb9f6ca),  //  #b9f6ca
        QColor(0x69f0ae),  //  #69f0ae
        QColor(0x00e676),  //  #00e676
        QColor(0x00c853),  //  #00c853
        QColor(0xf1f8e9),  //  #f1f8e9 // lightGreen
        QColor(0xdcedc8),  //  #dcedc8
        QColor(0xc5e1a5),  //  #c5e1a5
        QColor(0xaed581),  //  #aed581
        QColor(0x9ccc65),  //  #9ccc65
        QColor(0x8bc34a),  //  #8bc34a
        QColor(0x7cb342),  //  #7cb342
        QColor(0x689f38),  //  #689f38
        QColor(0x558b2f),  //  #558b2f
        QColor(0x33691e),  //  #33691e
        QColor(0xccff90),  //  #ccff90
        QColor(0xb2ff59),  //  #b2ff59
        QColor(0x76ff03),  //  #76ff03
        QColor(0x64dd17),  //  #64dd17
        QColor(0xf9fbe7),  //  #f9fbe7 // lime
        QColor(0xf0f4c3),  //  #f0f4c3
        QColor(0xe6ee9c),  //  #e6ee9c
        QColor(0xdce775),  //  #dce775
        QColor(0xd4e157),  //  #d4e157
        QColor(0xcddc39),  //  #cddc39
        QColor(0xc0ca33),  //  #c0ca33
        QColor(0xafb42b),  //  #afb42b
        QColor(0x9e9d24),  //  #9e9d24
        QColor(0x827717),  //  #827717
        QColor(0xf4ff81),  //  #f4ff81
        QColor(0xeeff41),  //  #eeff41
        QColor(0xc6ff00),  //  #c6ff00
        QColor(0xaeea00),  //  #aeea00
        QColor(0xfffde7),  //  #fffde7 // yellow
        QColor(0xfff9c4),  //  #fff9c4
        QColor(0xfff59d),  //  #fff59d
        QColor(0xfff176),  //  #fff176
        QColor(0xffee58),  //  #ffee58
        QColor(0xffeb3b),  //  #ffeb3b
        QColor(0xfdd835),  //  #fdd835
        QColor(0xfbc02d),  //  #fbc02d
        QColor(0xf9a825),  //  #f9a825
        QColor(0xf57f17),  //  #f57f17
        QColor(0xffff8d),  //  #ffff8d
        QColor(0xffff00),  //  #ffff00
        QColor(0xffea00),  //  #ffea00
        QColor(0xffd600),  //  #ffd600
        QColor(0xfff8e1),  //  #fff8e1 // amber
        QColor(0xffecb3),  //  #ffecb3
        QColor(0xffe082),  //  #ffe082
        QColor(0xffd54f),  //  #ffd54f
        QColor(0xffca28),  //  #ffca28
        QColor(0xffc107),  //  #ffc107
        QColor(0xffb300),  //  #ffb300
        QColor(0xffa000),  //  #ffa000
        QColor(0xff8f00),  //  #ff8f00
        QColor(0xff6f00),  //  #ff6f00
        QColor(0xffe57f),  //  #ffe57f
        QColor(0xffd740),  //  #ffd740
        QColor(0xffc400),  //  #ffc400
        QColor(0xffab00),  //  #ffab00
        QColor(0xfff3e0),  //  #fff3e0 // orange
        QColor(0xffe0b2),  //  #ffe0b2
        QColor(0xffcc80),  //  #ffcc80
        QColor(0xffb74d),  //  #ffb74d
        QColor(0xffa726),  //  #ffa726
        QColor(0xff9800),  //  #ff9800
        QColor(0xfb8c00),  //  #fb8c00
        QColor(0xf57c00),  //  #f57c00
        QColor(0xef6c00),  //  #ef6c00
        QColor(0xe65100),  //  #e65100
        QColor(0xffd180),  //  #ffd180
        QColor(0xffab40),  //  #ffab40
        QColor(0xff9100),  //  #ff9100
        QColor(0xff6d00),  //  #ff6d00
        QColor(0xfbe9e7),  //  #fbe9e7 // deepOrange
        QColor(0xffccbc),  //  #ffccbc
        QColor(0xffab91),  //  #ffab91
        QColor(0xff8a65),  //  #ff8a65
        QColor(0xff7043),  //  #ff7043
        QColor(0xff5722),  //  #ff5722
        QColor(0xf4511e),  //  #f4511e
        QColor(0xe64a19),  //  #e64a19
        QColor(0xd84315),  //  #d84315
        QColor(0xbf360c),  //  #bf360c
        QColor(0xff9e80),  //  #ff9e80
        QColor(0xff6e40),  //  #ff6e40
        QColor(0xff3d00),  //  #ff3d00
        QColor(0xdd2c00),  //  #dd2c00
        QColor(0xefebe9),  //  #efebe9 // brown
        QColor(0xd7ccc8),  //  #d7ccc8
        QColor(0xbcaaa4),  //  #bcaaa4
        QColor(0xa1887f),  //  #a1887f
        QColor(0x8d6e63),  //  #8d6e63
        QColor(0x795548),  //  #795548
        QColor(0x6d4c41),  //  #6d4c41
        QColor(0x5d4037),  //  #5d4037
        QColor(0x4e342e),  //  #4e342e
        QColor(0x3e2723),  //  #3e2723
        QColor(0xeceff1),  //  #eceff1 // blueGrey
        QColor(0xcfd8dc),  //  #cfd8dc
        QColor(0xb0bec5),  //  #b0bec5
        QColor(0x90a4ae),  //  #90a4ae
        QColor(0x78909c),  //  #78909c
        QColor(0x607d8b),  //  #607d8b
        QColor(0x546e7a),  //  #546e7a
        QColor(0x455a64),  //  #455a64
        QColor(0x37474f),  //  #37474f
        QColor(0x263238),  //  #263238
        QColor(0xfafafa),  //  #fafafa // grey
        QColor(0xf5f5f5),  //  #f5f5f5
        QColor(0xeeeeee),  //  #eeeeee
        QColor(0xe0e0e0),  //  #e0e0e0
        QColor(0xbdbdbd),  //  #bdbdbd
        QColor(0x9e9e9e),  //  #9e9e9e
        QColor(0x757575),  //  #757575
        QColor(0x616161),  //  #616161
        QColor(0x424242),  //  #424242
        QColor(0x212121),  //  #212121
        QColor(Qt::black), //  #000000 // other
        QColor(0xffffff),  //  #ffffff
        rgba(0, 0, 0, 0),
        rgba(0, 0, 0, 0.87),
        rgba(0, 0, 0, 0.54),
        rgba(0, 0, 0, 0.26),
        rgba(0, 0, 0, 0.12),
        rgba(255, 255, 255, 0.87),
        rgba(255, 255, 255, 0.54),
    };
    return palette[color];
}

QColor MatTheme::rgba(int r, int g, int b, qreal a)
{
    QColor color(r, g, b);
    color.setAlphaF(a);
    return color;
}

QX_MATERIAL_END_NAMESPACE
